package importing;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class importing {

 public void insertBlob(String imageId, String fileName) {
   Connection conn = null;
   try {
     conn = getConnection();
     if (!fileName.equals("")) {
       PreparedStatement ps = conn.prepareStatement("UPDATE FILM SET KEP = ? WHERE FILMID = ?");
       ps.setString(2, imageId);
       FileInputStream fis = new FileInputStream(fileName);
       ps.setBinaryStream(1, fis, fis.available());
       ps.execute();
       ps.close();
     } else {
       PreparedStatement ps = conn.prepareStatement("UPDATE FILM SET KEP = empty_blob() WHERE FILMID = ?");
       ps.setString(1, imageId);
       ps.execute();
       ps.close();

     }
     conn.close();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
 
 public void insertBlob2(String imageId, String fileName) {
	   Connection conn = null;
	   try {
	     conn = getConnection();
	     if (!fileName.equals("")) {
	       PreparedStatement ps = conn.prepareStatement("UPDATE CIKK SET KEP = ? WHERE ID = ?");
	       ps.setString(2, imageId);
	       FileInputStream fis = new FileInputStream(fileName);
	       ps.setBinaryStream(1, fis, fis.available());
	       ps.execute();
	       ps.close();
	     } else {
	       PreparedStatement ps = conn.prepareStatement("UPDATE CIKK SET KEP = empty_blob() WHERE ID = ?");
	       ps.setString(1, imageId);
	       ps.execute();
	       ps.close();

	     }
	     conn.close();
	   } catch (Exception e) {
	     e.printStackTrace();
	   }
	 }


 private Connection getConnection() throws ClassNotFoundException, SQLException {
   Class.forName("oracle.jdbc.driver.OracleDriver");
   Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "kino", "admin01");
   return conn;
 }

 public static void main(String[] args) throws InterruptedException {


  
   importing blobTest = new importing();
   
   File dir = new File("php/posters");
   String[] files = dir.list();
   for(int i=1; i<files.length; i++){
   blobTest.insertBlob(Integer.toString(i), "php/posters/" + i + ".jpg");
   System.out.println(i + ". filmkép beillesztve");
   Thread.sleep(1000);
   
   }
   
   dir = new File("php/cikk");
   files = dir.list();
   for(int i=1; i<=files.length; i++){
   blobTest.insertBlob2(Integer.toString(i), "php/cikk/" + i + ".jpg");
   System.out.println(i + ". cikk kép beillesztve");
   Thread.sleep(1000);
   }
   
   System.out.println("Képek importálása kész!");
 }
}

